<!-- HTML header for doxygen 1.8.6-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<title>OpenCV: Basic Thresholding Operations</title>
<link href="../../opencv.ico" rel="shortcut icon" type="image/x-icon" />
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../jquery.js"></script>
<script type="text/javascript" src="../../dynsections.js"></script>
<script type="text/javascript" src="../../tutorial-utils.js"></script>
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../search/searchdata.js"></script>
<script type="text/javascript" src="../../search/search.js"></script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
    jax: ["input/TeX","output/HTML-CSS"],
});
//<![CDATA[
MathJax.Hub.Config(
{
  TeX: {
      Macros: {
          matTT: [ "\\[ \\left|\\begin{array}{ccc} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{array}\\right| \\]", 9],
          fork: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ \\end{array} \\right.", 4],
          forkthree: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ \\end{array} \\right.", 6],
          forkfour: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ #7 & \\mbox{#8}\\\\ \\end{array} \\right.", 8],
          vecthree: ["\\begin{bmatrix} #1\\\\ #2\\\\ #3 \\end{bmatrix}", 3],
          vecthreethree: ["\\begin{bmatrix} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{bmatrix}", 9],
          cameramatrix: ["#1 = \\begin{bmatrix} f_x & 0 & c_x\\\\ 0 & f_y & c_y\\\\ 0 & 0 & 1 \\end{bmatrix}", 1],
          distcoeffs: ["(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6 [, s_1, s_2, s_3, s_4[, \\tau_x, \\tau_y]]]]) \\text{ of 4, 5, 8, 12 or 14 elements}"],
          distcoeffsfisheye: ["(k_1, k_2, k_3, k_4)"],
          hdotsfor: ["\\dots", 1],
          mathbbm: ["\\mathbb{#1}", 1],
          bordermatrix: ["\\matrix{#1}", 1]
      }
  }
}
);
//]]>
</script><script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js"></script>
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
<link href="../../stylesheet.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<!--#include virtual="/google-search.html"-->
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="../../opencv-logo-small.png"/></td>
  <td style="padding-left: 0.5em;">
   <div id="projectname">OpenCV
   &#160;<span id="projectnumber">4.5.2</span>
   </div>
   <div id="projectbrief">Open Source Computer Vision</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "../../search",false,'Search');
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('../../',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="../../d9/df8/tutorial_root.html">OpenCV Tutorials</a></li><li class="navelem"><a class="el" href="../../d7/da8/tutorial_table_of_content_imgproc.html">Image Processing (imgproc module)</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">Basic Thresholding Operations </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p><b>Prev Tutorial:</b> <a class="el" href="../../d4/d1f/tutorial_pyramids.html">Image Pyramids</a></p>
<p><b>Next Tutorial:</b> <a class="el" href="../../da/d97/tutorial_threshold_inRange.html">Thresholding Operations using inRange</a></p>
<table class="doxtable">
<tr>
<th align="right"></th><th align="left"></th></tr>
<tr>
<td align="right">Original author </td><td align="left">Ana Huamán </td></tr>
<tr>
<td align="right">Compatibility </td><td align="left">OpenCV &gt;= 3.0 </td></tr>
</table>
<h2>Goal </h2>
<p>In this tutorial you will learn how to:</p>
<ul>
<li>Perform basic thresholding operations using OpenCV function <a class="el" href="../../d7/d1b/group__imgproc__misc.html#gae8a4a146d1ca78c626a53577199e9c57">cv::threshold</a></li>
</ul>
<h2>Cool Theory </h2>
<dl class="section note"><dt>Note</dt><dd>The explanation below belongs to the book <b>Learning OpenCV</b> by Bradski and Kaehler. What is</dd></dl>
<h2>Thresholding? </h2>
<ul>
<li>The simplest segmentation method</li>
<li>Application example: Separate out regions of an image corresponding to objects which we want to analyze. This separation is based on the variation of intensity between the object pixels and the background pixels.</li>
<li>To differentiate the pixels we are interested in from the rest (which will eventually be rejected), we perform a comparison of each pixel intensity value with respect to a <em>threshold</em> (determined according to the problem to solve).</li>
<li><p class="startli">Once we have separated properly the important pixels, we can set them with a determined value to identify them (i.e. we can assign them a value of \(0\) (black), \(255\) (white) or any value that suits your needs).</p>
<div class="image">
<img src="../../Threshold_Tutorial_Theory_Example.jpg" alt="Threshold_Tutorial_Theory_Example.jpg"/>
</div>
</li>
</ul>
<h3>Types of Thresholding</h3>
<ul>
<li>OpenCV offers the function <a class="el" href="../../d7/d1b/group__imgproc__misc.html#gae8a4a146d1ca78c626a53577199e9c57">cv::threshold</a> to perform thresholding operations.</li>
<li>We can effectuate \(5\) types of Thresholding operations with this function. We will explain them in the following subsections.</li>
<li><p class="startli">To illustrate how these thresholding processes work, let's consider that we have a source image with pixels with intensity values \(src(x,y)\). The plot below depicts this. The horizontal blue line represents the threshold \(thresh\) (fixed).</p>
<div class="image">
<img src="../../Threshold_Tutorial_Theory_Base_Figure.png" alt="Threshold_Tutorial_Theory_Base_Figure.png"/>
</div>
</li>
</ul>
<h4>Threshold Binary</h4>
<ul>
<li><p class="startli">This thresholding operation can be expressed as:</p>
<p class="formulaDsp">
\[\texttt{dst} (x,y) = \fork{\texttt{maxVal}}{if \(\texttt{src}(x,y) &gt; \texttt{thresh}\)}{0}{otherwise}\]
</p>
</li>
<li><p class="startli">So, if the intensity of the pixel \(src(x,y)\) is higher than \(thresh\), then the new pixel intensity is set to a \(MaxVal\). Otherwise, the pixels are set to \(0\).</p>
<div class="image">
<img src="../../Threshold_Tutorial_Theory_Binary.png" alt="Threshold_Tutorial_Theory_Binary.png"/>
</div>
</li>
</ul>
<h4>Threshold Binary, Inverted</h4>
<ul>
<li><p class="startli">This thresholding operation can be expressed as:</p>
<p class="formulaDsp">
\[\texttt{dst} (x,y) = \fork{0}{if \(\texttt{src}(x,y) &gt; \texttt{thresh}\)}{\texttt{maxVal}}{otherwise}\]
</p>
</li>
<li><p class="startli">If the intensity of the pixel \(src(x,y)\) is higher than \(thresh\), then the new pixel intensity is set to a \(0\). Otherwise, it is set to \(MaxVal\).</p>
<div class="image">
<img src="../../Threshold_Tutorial_Theory_Binary_Inverted.png" alt="Threshold_Tutorial_Theory_Binary_Inverted.png"/>
</div>
</li>
</ul>
<h4>Truncate</h4>
<ul>
<li><p class="startli">This thresholding operation can be expressed as:</p>
<p class="formulaDsp">
\[\texttt{dst} (x,y) = \fork{\texttt{threshold}}{if \(\texttt{src}(x,y) &gt; \texttt{thresh}\)}{\texttt{src}(x,y)}{otherwise}\]
</p>
</li>
<li><p class="startli">The maximum intensity value for the pixels is \(thresh\), if \(src(x,y)\) is greater, then its value is <em>truncated</em>. See figure below:</p>
<div class="image">
<img src="../../Threshold_Tutorial_Theory_Truncate.png" alt="Threshold_Tutorial_Theory_Truncate.png"/>
</div>
</li>
</ul>
<h4>Threshold to Zero</h4>
<ul>
<li><p class="startli">This operation can be expressed as:</p>
<p class="formulaDsp">
\[\texttt{dst} (x,y) = \fork{\texttt{src}(x,y)}{if \(\texttt{src}(x,y) &gt; \texttt{thresh}\)}{0}{otherwise}\]
</p>
</li>
<li><p class="startli">If \(src(x,y)\) is lower than \(thresh\), the new pixel value will be set to \(0\).</p>
<div class="image">
<img src="../../Threshold_Tutorial_Theory_Zero.png" alt="Threshold_Tutorial_Theory_Zero.png"/>
</div>
</li>
</ul>
<h4>Threshold to Zero, Inverted</h4>
<ul>
<li><p class="startli">This operation can be expressed as:</p>
<p class="formulaDsp">
\[\texttt{dst} (x,y) = \fork{0}{if \(\texttt{src}(x,y) &gt; \texttt{thresh}\)}{\texttt{src}(x,y)}{otherwise}\]
</p>
</li>
<li><p class="startli">If \(src(x,y)\) is greater than \(thresh\), the new pixel value will be set to \(0\).</p>
<div class="image">
<img src="../../Threshold_Tutorial_Theory_Zero_Inverted.png" alt="Threshold_Tutorial_Theory_Zero_Inverted.png"/>
</div>
</li>
</ul>
<h2>Code </h2>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'><p> The tutorial code's is shown lines below. You can also download it from <a href="https://github.com/opencv/opencv/tree/master/samples/cpp/tutorial_code/ImgProc/Threshold.cpp">here</a> </p><div class="fragment"><div class="line"></div><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="../../d1/d4f/imgproc_2include_2opencv2_2imgproc_8hpp.html">opencv2/imgproc.hpp</a>&quot;</span></div><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="../../d6/d87/imgcodecs_8hpp.html">opencv2/imgcodecs.hpp</a>&quot;</span></div><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="../../d4/dd5/highgui_8hpp.html">opencv2/highgui.hpp</a>&quot;</span></div><div class="line"><span class="preprocessor">#include &lt;iostream&gt;</span></div><div class="line"></div><div class="line"><span class="keyword">using namespace </span><a class="code" href="../../d2/d75/namespacecv.html">cv</a>;</div><div class="line"><span class="keyword">using</span> std::cout;</div><div class="line"></div><div class="line"></div><div class="line"><span class="keywordtype">int</span> threshold_value = 0;</div><div class="line"><span class="keywordtype">int</span> threshold_type = 3;</div><div class="line"><span class="keywordtype">int</span> <span class="keyword">const</span> max_value = 255;</div><div class="line"><span class="keywordtype">int</span> <span class="keyword">const</span> max_type = 4;</div><div class="line"><span class="keywordtype">int</span> <span class="keyword">const</span> max_binary_value = 255;</div><div class="line"></div><div class="line"><a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> src, src_gray, dst;</div><div class="line"><span class="keyword">const</span> <span class="keywordtype">char</span>* window_name = <span class="stringliteral">&quot;Threshold Demo&quot;</span>;</div><div class="line"></div><div class="line"><span class="keyword">const</span> <span class="keywordtype">char</span>* trackbar_type = <span class="stringliteral">&quot;Type: \n 0: Binary \n 1: Binary Inverted \n 2: Truncate \n 3: To Zero \n 4: To Zero Inverted&quot;</span>;</div><div class="line"><span class="keyword">const</span> <span class="keywordtype">char</span>* trackbar_value = <span class="stringliteral">&quot;Value&quot;</span>;</div><div class="line"></div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> Threshold_Demo( <span class="keywordtype">int</span>, <span class="keywordtype">void</span>* )</div><div class="line">{</div><div class="line">    <span class="comment">/* 0: Binary</span></div><div class="line"><span class="comment">     1: Binary Inverted</span></div><div class="line"><span class="comment">     2: Threshold Truncated</span></div><div class="line"><span class="comment">     3: Threshold to Zero</span></div><div class="line"><span class="comment">     4: Threshold to Zero Inverted</span></div><div class="line"><span class="comment">    */</span></div><div class="line">    <a class="code" href="../../d7/d1b/group__imgproc__misc.html#gae8a4a146d1ca78c626a53577199e9c57">threshold</a>( src_gray, dst, threshold_value, max_binary_value, threshold_type );</div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#ga453d42fe4cb60e5723281a89973ee563">imshow</a>( window_name, dst );</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main( <span class="keywordtype">int</span> argc, <span class="keywordtype">char</span>** argv )</div><div class="line">{</div><div class="line">    <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> imageName(<span class="stringliteral">&quot;stuff.jpg&quot;</span>); <span class="comment">// by default</span></div><div class="line">    <span class="keywordflow">if</span> (argc &gt; 1)</div><div class="line">    {</div><div class="line">        imageName = argv[1];</div><div class="line">    }</div><div class="line">    src = <a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">imread</a>( <a class="code" href="../../d6/dba/group__core__utils__samples.html#ga3a33b00033b46c698ff6340d95569c13">samples::findFile</a>( imageName ), <a class="code" href="../../d8/d6a/group__imgcodecs__flags.html#gga61d9b0126a3e57d9277ac48327799c80af660544735200cbe942eea09232eb822">IMREAD_COLOR</a> ); <span class="comment">// Load an image</span></div><div class="line"></div><div class="line">    <span class="keywordflow">if</span> (src.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#abbec3525a852e77998aba034813fded4">empty</a>())</div><div class="line">    {</div><div class="line">        cout &lt;&lt; <span class="stringliteral">&quot;Cannot read the image: &quot;</span> &lt;&lt; imageName &lt;&lt; std::endl;</div><div class="line">        <span class="keywordflow">return</span> -1;</div><div class="line">    }</div><div class="line"></div><div class="line">    <a class="code" href="../../d8/d01/group__imgproc__color__conversions.html#ga397ae87e1288a81d2363b61574eb8cab">cvtColor</a>( src, src_gray, <a class="code" href="../../d8/d01/group__imgproc__color__conversions.html#gga4e0972be5de079fed4e3a10e24ef5ef0a353a4b8db9040165db4dacb5bcefb6ea">COLOR_BGR2GRAY</a> ); <span class="comment">// Convert the image to Gray</span></div><div class="line"><span class="comment"></span></div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#ga5afdf8410934fd099df85c75b2e0888b">namedWindow</a>( window_name, <a class="code" href="../../d0/d90/group__highgui__window__flags.html#ggabf7d2c5625bc59ac130287f925557ac3acf621ace7a54954cbac01df27e47228f">WINDOW_AUTOSIZE</a> ); <span class="comment">// Create a window to display results</span></div><div class="line"><span class="comment"></span></div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#gaf78d2155d30b728fc413803745b67a9b">createTrackbar</a>( trackbar_type,</div><div class="line">                    window_name, &amp;threshold_type,</div><div class="line">                    max_type, Threshold_Demo ); <span class="comment">// Create a Trackbar to choose type of Threshold</span></div><div class="line"></div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#gaf78d2155d30b728fc413803745b67a9b">createTrackbar</a>( trackbar_value,</div><div class="line">                    window_name, &amp;threshold_value,</div><div class="line">                    max_value, Threshold_Demo ); <span class="comment">// Create a Trackbar to choose Threshold value</span></div><div class="line"><span class="comment"></span></div><div class="line">    Threshold_Demo( 0, 0 ); <span class="comment">// Call the function to initialize</span></div><div class="line"></div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#ga5628525ad33f52eab17feebcfba38bd7">waitKey</a>();</div><div class="line">    <span class="keywordflow">return</span> 0;</div><div class="line">}</div></div><!-- fragment -->  </div>  <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'><p> The tutorial code's is shown lines below. You can also download it from <a href="https://github.com/opencv/opencv/tree/master/samples/java/tutorial_code/ImgProc/threshold/Threshold.java">here</a> </p><div class="fragment"><div class="line"><span class="keyword">import</span> java.awt.BorderLayout;</div><div class="line"><span class="keyword">import</span> java.awt.Container;</div><div class="line"><span class="keyword">import</span> java.awt.Image;</div><div class="line"></div><div class="line"><span class="keyword">import</span> javax.swing.BoxLayout;</div><div class="line"><span class="keyword">import</span> javax.swing.ImageIcon;</div><div class="line"><span class="keyword">import</span> javax.swing.JFrame;</div><div class="line"><span class="keyword">import</span> javax.swing.JLabel;</div><div class="line"><span class="keyword">import</span> javax.swing.JPanel;</div><div class="line"><span class="keyword">import</span> javax.swing.JSlider;</div><div class="line"><span class="keyword">import</span> javax.swing.event.ChangeEvent;</div><div class="line"><span class="keyword">import</span> javax.swing.event.ChangeListener;</div><div class="line"></div><div class="line"><span class="keyword">import</span> org.opencv.core.Core;</div><div class="line"><span class="keyword">import</span> org.opencv.core.Mat;</div><div class="line"><span class="keyword">import</span> org.opencv.highgui.HighGui;</div><div class="line"><span class="keyword">import</span> org.opencv.imgcodecs.Imgcodecs;</div><div class="line"><span class="keyword">import</span> org.opencv.imgproc.Imgproc;</div><div class="line"></div><div class="line"><span class="keyword">public</span> <span class="keyword">class </span>Threshold {</div><div class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keywordtype">int</span> MAX_VALUE = 255;</div><div class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keywordtype">int</span> MAX_TYPE = 4;</div><div class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keywordtype">int</span> MAX_BINARY_VALUE = 255;</div><div class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> WINDOW_NAME = <span class="stringliteral">&quot;Threshold Demo&quot;</span>;</div><div class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> TRACKBAR_TYPE = <span class="stringliteral">&quot;&lt;html&gt;&lt;body&gt;Type: &lt;br&gt; 0: Binary &lt;br&gt; &quot;</span></div><div class="line">            + <span class="stringliteral">&quot;1: Binary Inverted &lt;br&gt; 2: Truncate &lt;br&gt; &quot;</span></div><div class="line">            + <span class="stringliteral">&quot;3: To Zero &lt;br&gt; 4: To Zero Inverted&lt;/body&gt;&lt;/html&gt;&quot;</span>;</div><div class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> TRACKBAR_VALUE = <span class="stringliteral">&quot;Value&quot;</span>;</div><div class="line">    <span class="keyword">private</span> <span class="keywordtype">int</span> thresholdValue = 0;</div><div class="line">    <span class="keyword">private</span> <span class="keywordtype">int</span> thresholdType = 3;</div><div class="line">    <span class="keyword">private</span> Mat src;</div><div class="line">    <span class="keyword">private</span> Mat srcGray = <span class="keyword">new</span> Mat();</div><div class="line">    <span class="keyword">private</span> Mat dst = <span class="keyword">new</span> Mat();</div><div class="line">    <span class="keyword">private</span> JFrame frame;</div><div class="line">    <span class="keyword">private</span> JLabel imgLabel;</div><div class="line"></div><div class="line">    <span class="keyword">public</span> Threshold(<a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a>[] args) {</div><div class="line">        <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> imagePath = <span class="stringliteral">&quot;../data/stuff.jpg&quot;</span>;</div><div class="line">        <span class="keywordflow">if</span> (args.length &gt; 0) {</div><div class="line">            imagePath = args[0];</div><div class="line">        }</div><div class="line">        <span class="comment">// Load an image</span></div><div class="line">        src = Imgcodecs.imread(imagePath);</div><div class="line">        <span class="keywordflow">if</span> (src.empty()) {</div><div class="line">            System.out.println(<span class="stringliteral">&quot;Empty image: &quot;</span> + imagePath);</div><div class="line">            System.exit(0);</div><div class="line">        }</div><div class="line">        <span class="comment">// Convert the image to Gray</span></div><div class="line">        Imgproc.cvtColor(src, srcGray, Imgproc.COLOR_BGR2GRAY);</div><div class="line"></div><div class="line">        <span class="comment">// Create and set up the window.</span></div><div class="line">        frame = <span class="keyword">new</span> JFrame(WINDOW_NAME);</div><div class="line">        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);</div><div class="line">        <span class="comment">// Set up the content pane.</span></div><div class="line">        Image img = HighGui.toBufferedImage(srcGray);</div><div class="line">        addComponentsToPane(frame.getContentPane(), img);</div><div class="line">        <span class="comment">// Use the content pane&#39;s default BorderLayout. No need for</span></div><div class="line">        <span class="comment">// setLayout(new BorderLayout());</span></div><div class="line">        <span class="comment">// Display the window.</span></div><div class="line">        frame.pack();</div><div class="line">        frame.setVisible(<span class="keyword">true</span>);</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="keyword">private</span> <span class="keywordtype">void</span> addComponentsToPane(Container pane, Image img) {</div><div class="line">        <span class="keywordflow">if</span> (!(pane.getLayout() instanceof BorderLayout)) {</div><div class="line">            pane.add(<span class="keyword">new</span> JLabel(<span class="stringliteral">&quot;Container doesn&#39;t use BorderLayout!&quot;</span>));</div><div class="line">            <span class="keywordflow">return</span>;</div><div class="line">        }</div><div class="line"></div><div class="line">        JPanel sliderPanel = <span class="keyword">new</span> JPanel();</div><div class="line">        sliderPanel.setLayout(<span class="keyword">new</span> BoxLayout(sliderPanel, BoxLayout.PAGE_AXIS));</div><div class="line"></div><div class="line">        sliderPanel.add(<span class="keyword">new</span> JLabel(TRACKBAR_TYPE));</div><div class="line">        <span class="comment">// Create Trackbar to choose type of Threshold</span></div><div class="line">        JSlider sliderThreshType = <span class="keyword">new</span> JSlider(0, MAX_TYPE, thresholdType);</div><div class="line">        sliderThreshType.setMajorTickSpacing(1);</div><div class="line">        sliderThreshType.setMinorTickSpacing(1);</div><div class="line">        sliderThreshType.setPaintTicks(<span class="keyword">true</span>);</div><div class="line">        sliderThreshType.setPaintLabels(<span class="keyword">true</span>);</div><div class="line">        sliderPanel.add(sliderThreshType);</div><div class="line"></div><div class="line">        sliderPanel.add(<span class="keyword">new</span> JLabel(TRACKBAR_VALUE));</div><div class="line">        <span class="comment">// Create Trackbar to choose Threshold value</span></div><div class="line">        JSlider sliderThreshValue = <span class="keyword">new</span> JSlider(0, MAX_VALUE, 0);</div><div class="line">        sliderThreshValue.setMajorTickSpacing(50);</div><div class="line">        sliderThreshValue.setMinorTickSpacing(10);</div><div class="line">        sliderThreshValue.setPaintTicks(<span class="keyword">true</span>);</div><div class="line">        sliderThreshValue.setPaintLabels(<span class="keyword">true</span>);</div><div class="line">        sliderPanel.add(sliderThreshValue);</div><div class="line"></div><div class="line">        sliderThreshType.addChangeListener(<span class="keyword">new</span> ChangeListener() {</div><div class="line">            @Override</div><div class="line">            <span class="keyword">public</span> <span class="keywordtype">void</span> stateChanged(ChangeEvent e) {</div><div class="line">                JSlider source = (JSlider) e.getSource();</div><div class="line">                thresholdType = source.getValue();</div><div class="line">                update();</div><div class="line">            }</div><div class="line">        });</div><div class="line"></div><div class="line">        sliderThreshValue.addChangeListener(<span class="keyword">new</span> ChangeListener() {</div><div class="line">            @Override</div><div class="line">            <span class="keyword">public</span> <span class="keywordtype">void</span> stateChanged(ChangeEvent e) {</div><div class="line">                JSlider source = (JSlider) e.getSource();</div><div class="line">                thresholdValue = source.getValue();</div><div class="line">                update();</div><div class="line">            }</div><div class="line">        });</div><div class="line"></div><div class="line">        pane.add(sliderPanel, BorderLayout.PAGE_START);</div><div class="line">        imgLabel = <span class="keyword">new</span> JLabel(<span class="keyword">new</span> ImageIcon(img));</div><div class="line">        pane.add(imgLabel, BorderLayout.CENTER);</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="keyword">private</span> <span class="keywordtype">void</span> update() {</div><div class="line">        Imgproc.threshold(srcGray, dst, thresholdValue, MAX_BINARY_VALUE, thresholdType);</div><div class="line">        Image img = HighGui.toBufferedImage(dst);</div><div class="line">        imgLabel.setIcon(<span class="keyword">new</span> ImageIcon(img));</div><div class="line">        frame.repaint();</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keywordtype">void</span> main(<a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a>[] args) {</div><div class="line">        <span class="comment">// Load the native OpenCV library</span></div><div class="line">        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);</div><div class="line"></div><div class="line">        <span class="comment">// Schedule a job for the event dispatch thread:</span></div><div class="line">        <span class="comment">// creating and showing this application&#39;s GUI.</span></div><div class="line">        javax.swing.SwingUtilities.invokeLater(<span class="keyword">new</span> Runnable() {</div><div class="line">            @Override</div><div class="line">            <span class="keyword">public</span> <span class="keywordtype">void</span> run() {</div><div class="line">                <span class="keyword">new</span> Threshold(args);</div><div class="line">            }</div><div class="line">        });</div><div class="line">    }</div><div class="line">}</div></div><!-- fragment -->  </div>  <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'><p> The tutorial code's is shown lines below. You can also download it from <a href="https://github.com/opencv/opencv/tree/master/samples/python/tutorial_code/imgProc/threshold/threshold.py">here</a> </p><div class="fragment"><div class="line"><span class="keyword">from</span> __future__ <span class="keyword">import</span> print_function</div><div class="line"><span class="keyword">import</span> cv2 <span class="keyword">as</span> cv</div><div class="line"><span class="keyword">import</span> argparse</div><div class="line"></div><div class="line">max_value = 255</div><div class="line">max_type = 4</div><div class="line">max_binary_value = 255</div><div class="line">trackbar_type = <span class="stringliteral">&#39;Type: \n 0: Binary \n 1: Binary Inverted \n 2: Truncate \n 3: To Zero \n 4: To Zero Inverted&#39;</span></div><div class="line">trackbar_value = <span class="stringliteral">&#39;Value&#39;</span></div><div class="line">window_name = <span class="stringliteral">&#39;Threshold Demo&#39;</span></div><div class="line"></div><div class="line"></div><div class="line"><span class="keyword">def </span>Threshold_Demo(val):</div><div class="line">    <span class="comment">#0: Binary</span></div><div class="line">    <span class="comment">#1: Binary Inverted</span></div><div class="line">    <span class="comment">#2: Threshold Truncated</span></div><div class="line">    <span class="comment">#3: Threshold to Zero</span></div><div class="line">    <span class="comment">#4: Threshold to Zero Inverted</span></div><div class="line">    threshold_type = <a class="code" href="../../d7/dfc/group__highgui.html#ga122632e9e91b9ec06943472c55d9cda8">cv.getTrackbarPos</a>(trackbar_type, window_name)</div><div class="line">    threshold_value = <a class="code" href="../../d7/dfc/group__highgui.html#ga122632e9e91b9ec06943472c55d9cda8">cv.getTrackbarPos</a>(trackbar_value, window_name)</div><div class="line">    _, dst = <a class="code" href="../../d7/d1b/group__imgproc__misc.html#gae8a4a146d1ca78c626a53577199e9c57">cv.threshold</a>(src_gray, threshold_value, max_binary_value, threshold_type )</div><div class="line">    <a class="code" href="../../df/d24/group__highgui__opengl.html#gaae7e90aa3415c68dba22a5ff2cefc25d">cv.imshow</a>(window_name, dst)</div><div class="line"></div><div class="line"></div><div class="line">parser = argparse.ArgumentParser(description=<span class="stringliteral">&#39;Code for Basic Thresholding Operations tutorial.&#39;</span>)</div><div class="line">parser.add_argument(<span class="stringliteral">&#39;--input&#39;</span>, help=<span class="stringliteral">&#39;Path to input image.&#39;</span>, default=<span class="stringliteral">&#39;stuff.jpg&#39;</span>)</div><div class="line">args = parser.parse_args()</div><div class="line"></div><div class="line"></div><div class="line">src = <a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">cv.imread</a>(<a class="code" href="../../d6/dba/group__core__utils__samples.html#ga3a33b00033b46c698ff6340d95569c13">cv.samples.findFile</a>(args.input))</div><div class="line"><span class="keywordflow">if</span> src <span class="keywordflow">is</span> <span class="keywordtype">None</span>:</div><div class="line">    <a class="code" href="../../df/d57/namespacecv_1_1dnn.html#a701210a0203f2786cbfd04b2bd56da47">print</a>(<span class="stringliteral">&#39;Could not open or find the image: &#39;</span>, args.input)</div><div class="line">    exit(0)</div><div class="line"><span class="comment"># Convert the image to Gray</span></div><div class="line">src_gray = <a class="code" href="../../d8/d01/group__imgproc__color__conversions.html#ga397ae87e1288a81d2363b61574eb8cab">cv.cvtColor</a>(src, cv.COLOR_BGR2GRAY)</div><div class="line"></div><div class="line"></div><div class="line"></div><div class="line"><a class="code" href="../../d7/dfc/group__highgui.html#ga5afdf8410934fd099df85c75b2e0888b">cv.namedWindow</a>(window_name)</div><div class="line"></div><div class="line"></div><div class="line"></div><div class="line"><a class="code" href="../../d7/dfc/group__highgui.html#gaf78d2155d30b728fc413803745b67a9b">cv.createTrackbar</a>(trackbar_type, window_name , 3, max_type, Threshold_Demo)</div><div class="line"><span class="comment"># Create Trackbar to choose Threshold value</span></div><div class="line"><a class="code" href="../../d7/dfc/group__highgui.html#gaf78d2155d30b728fc413803745b67a9b">cv.createTrackbar</a>(trackbar_value, window_name , 0, max_value, Threshold_Demo)</div><div class="line"></div><div class="line"></div><div class="line"><span class="comment"># Call the function to initialize</span></div><div class="line">Threshold_Demo(0)</div><div class="line"><span class="comment"># Wait until user finishes program</span></div><div class="line"><a class="code" href="../../d7/dfc/group__highgui.html#ga5628525ad33f52eab17feebcfba38bd7">cv.waitKey</a>()</div></div><!-- fragment -->  </div> <h2>Explanation </h2>
<p>Let's check the general structure of the program:</p><ul>
<li>Load an image. If it is BGR we convert it to Grayscale. For this, remember that we can use the function <a class="el" href="../../d8/d01/group__imgproc__color__conversions.html#ga397ae87e1288a81d2363b61574eb8cab">cv::cvtColor</a> :</li>
</ul>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'> <div class="fragment"><div class="line">    <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> imageName(<span class="stringliteral">&quot;stuff.jpg&quot;</span>); <span class="comment">// by default</span></div><div class="line">    <span class="keywordflow">if</span> (argc &gt; 1)</div><div class="line">    {</div><div class="line">        imageName = argv[1];</div><div class="line">    }</div><div class="line">    src = <a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">imread</a>( <a class="code" href="../../d6/dba/group__core__utils__samples.html#ga3a33b00033b46c698ff6340d95569c13">samples::findFile</a>( imageName ), <a class="code" href="../../d8/d6a/group__imgcodecs__flags.html#gga61d9b0126a3e57d9277ac48327799c80af660544735200cbe942eea09232eb822">IMREAD_COLOR</a> ); <span class="comment">// Load an image</span></div><div class="line"></div><div class="line">    <span class="keywordflow">if</span> (src.empty())</div><div class="line">    {</div><div class="line">        cout &lt;&lt; <span class="stringliteral">&quot;Cannot read the image: &quot;</span> &lt;&lt; imageName &lt;&lt; std::endl;</div><div class="line">        <span class="keywordflow">return</span> -1;</div><div class="line">    }</div><div class="line"></div><div class="line">    <a class="code" href="../../d8/d01/group__imgproc__color__conversions.html#ga397ae87e1288a81d2363b61574eb8cab">cvtColor</a>( src, src_gray, <a class="code" href="../../d8/d01/group__imgproc__color__conversions.html#gga4e0972be5de079fed4e3a10e24ef5ef0a353a4b8db9040165db4dacb5bcefb6ea">COLOR_BGR2GRAY</a> ); <span class="comment">// Convert the image to Gray</span></div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'> <div class="fragment"><div class="line">        <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> imagePath = <span class="stringliteral">&quot;../data/stuff.jpg&quot;</span>;</div><div class="line">        <span class="keywordflow">if</span> (args.length &gt; 0) {</div><div class="line">            imagePath = args[0];</div><div class="line">        }</div><div class="line">        <span class="comment">// Load an image</span></div><div class="line">        src = Imgcodecs.imread(imagePath);</div><div class="line">        <span class="keywordflow">if</span> (src.empty()) {</div><div class="line">            System.out.println(<span class="stringliteral">&quot;Empty image: &quot;</span> + imagePath);</div><div class="line">            System.exit(0);</div><div class="line">        }</div><div class="line">        <span class="comment">// Convert the image to Gray</span></div><div class="line">        Imgproc.cvtColor(src, srcGray, Imgproc.COLOR_BGR2GRAY);</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'> <div class="fragment"><div class="line"><span class="comment"># Load an image</span></div><div class="line">src = <a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">cv.imread</a>(<a class="code" href="../../d6/dba/group__core__utils__samples.html#ga3a33b00033b46c698ff6340d95569c13">cv.samples.findFile</a>(args.input))</div><div class="line"><span class="keywordflow">if</span> src <span class="keywordflow">is</span> <span class="keywordtype">None</span>:</div><div class="line">    <a class="code" href="../../df/d57/namespacecv_1_1dnn.html#a701210a0203f2786cbfd04b2bd56da47">print</a>(<span class="stringliteral">&#39;Could not open or find the image: &#39;</span>, args.input)</div><div class="line">    exit(0)</div><div class="line"><span class="comment"># Convert the image to Gray</span></div><div class="line">src_gray = <a class="code" href="../../d8/d01/group__imgproc__color__conversions.html#ga397ae87e1288a81d2363b61574eb8cab">cv.cvtColor</a>(src, cv.COLOR_BGR2GRAY)</div></div><!-- fragment --> </div> <ul>
<li>Create a window to display the result</li>
</ul>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'> <div class="fragment"><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#ga5afdf8410934fd099df85c75b2e0888b">namedWindow</a>( window_name, <a class="code" href="../../d0/d90/group__highgui__window__flags.html#ggabf7d2c5625bc59ac130287f925557ac3acf621ace7a54954cbac01df27e47228f">WINDOW_AUTOSIZE</a> ); <span class="comment">// Create a window to display results</span></div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'> <div class="fragment"><div class="line">        <span class="comment">// Create and set up the window.</span></div><div class="line">        frame = <span class="keyword">new</span> JFrame(WINDOW_NAME);</div><div class="line">        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);</div><div class="line">        <span class="comment">// Set up the content pane.</span></div><div class="line">        Image img = HighGui.toBufferedImage(srcGray);</div><div class="line">        addComponentsToPane(frame.getContentPane(), img);</div><div class="line">        <span class="comment">// Use the content pane&#39;s default BorderLayout. No need for</span></div><div class="line">        <span class="comment">// setLayout(new BorderLayout());</span></div><div class="line">        <span class="comment">// Display the window.</span></div><div class="line">        frame.pack();</div><div class="line">        frame.setVisible(<span class="keyword">true</span>);</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'> <div class="fragment"><div class="line"><span class="comment"># Create a window to display results</span></div><div class="line"><a class="code" href="../../d7/dfc/group__highgui.html#ga5afdf8410934fd099df85c75b2e0888b">cv.namedWindow</a>(window_name)</div></div><!-- fragment --> </div> <ul>
<li>Create \(2\) trackbars for the user to enter user input:<ul>
<li><b>Type of thresholding</b>: Binary, To Zero, etc...</li>
<li><b>Threshold value</b></li>
</ul>
</li>
</ul>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'> <div class="fragment"><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#gaf78d2155d30b728fc413803745b67a9b">createTrackbar</a>( trackbar_type,</div><div class="line">                    window_name, &amp;threshold_type,</div><div class="line">                    max_type, Threshold_Demo ); <span class="comment">// Create a Trackbar to choose type of Threshold</span></div><div class="line"></div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#gaf78d2155d30b728fc413803745b67a9b">createTrackbar</a>( trackbar_value,</div><div class="line">                    window_name, &amp;threshold_value,</div><div class="line">                    max_value, Threshold_Demo ); <span class="comment">// Create a Trackbar to choose Threshold value</span></div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'> <div class="fragment"><div class="line">        sliderPanel.add(<span class="keyword">new</span> JLabel(TRACKBAR_TYPE));</div><div class="line">        <span class="comment">// Create Trackbar to choose type of Threshold</span></div><div class="line">        JSlider sliderThreshType = <span class="keyword">new</span> JSlider(0, MAX_TYPE, thresholdType);</div><div class="line">        sliderThreshType.setMajorTickSpacing(1);</div><div class="line">        sliderThreshType.setMinorTickSpacing(1);</div><div class="line">        sliderThreshType.setPaintTicks(<span class="keyword">true</span>);</div><div class="line">        sliderThreshType.setPaintLabels(<span class="keyword">true</span>);</div><div class="line">        sliderPanel.add(sliderThreshType);</div><div class="line"></div><div class="line">        sliderPanel.add(<span class="keyword">new</span> JLabel(TRACKBAR_VALUE));</div><div class="line">        <span class="comment">// Create Trackbar to choose Threshold value</span></div><div class="line">        JSlider sliderThreshValue = <span class="keyword">new</span> JSlider(0, MAX_VALUE, 0);</div><div class="line">        sliderThreshValue.setMajorTickSpacing(50);</div><div class="line">        sliderThreshValue.setMinorTickSpacing(10);</div><div class="line">        sliderThreshValue.setPaintTicks(<span class="keyword">true</span>);</div><div class="line">        sliderThreshValue.setPaintLabels(<span class="keyword">true</span>);</div><div class="line">        sliderPanel.add(sliderThreshValue);</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'> <div class="fragment"><div class="line"><span class="comment"># Create Trackbar to choose type of Threshold</span></div><div class="line"><a class="code" href="../../d7/dfc/group__highgui.html#gaf78d2155d30b728fc413803745b67a9b">cv.createTrackbar</a>(trackbar_type, window_name , 3, max_type, Threshold_Demo)</div><div class="line"><span class="comment"># Create Trackbar to choose Threshold value</span></div><div class="line"><a class="code" href="../../d7/dfc/group__highgui.html#gaf78d2155d30b728fc413803745b67a9b">cv.createTrackbar</a>(trackbar_value, window_name , 0, max_value, Threshold_Demo)</div></div><!-- fragment --> </div> <ul>
<li>Wait until the user enters the threshold value, the type of thresholding (or until the program exits)</li>
<li>Whenever the user changes the value of any of the Trackbars, the function <em>Threshold_Demo</em> (<em>update</em> in Java) is called:</li>
</ul>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'> <div class="fragment"><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> Threshold_Demo( <span class="keywordtype">int</span>, <span class="keywordtype">void</span>* )</div><div class="line">{</div><div class="line">    <span class="comment">/* 0: Binary</span></div><div class="line"><span class="comment">     1: Binary Inverted</span></div><div class="line"><span class="comment">     2: Threshold Truncated</span></div><div class="line"><span class="comment">     3: Threshold to Zero</span></div><div class="line"><span class="comment">     4: Threshold to Zero Inverted</span></div><div class="line"><span class="comment">    */</span></div><div class="line">    <a class="code" href="../../d7/d1b/group__imgproc__misc.html#gae8a4a146d1ca78c626a53577199e9c57">threshold</a>( src_gray, dst, threshold_value, max_binary_value, threshold_type );</div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#ga453d42fe4cb60e5723281a89973ee563">imshow</a>( window_name, dst );</div><div class="line">}</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'> <div class="fragment"><div class="line">    <span class="keyword">private</span> <span class="keywordtype">void</span> update() {</div><div class="line">        Imgproc.threshold(srcGray, dst, thresholdValue, MAX_BINARY_VALUE, thresholdType);</div><div class="line">        Image img = HighGui.toBufferedImage(dst);</div><div class="line">        imgLabel.setIcon(<span class="keyword">new</span> ImageIcon(img));</div><div class="line">        frame.repaint();</div><div class="line">    }</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'> <div class="fragment"><div class="line"><span class="keyword">def </span>Threshold_Demo(val):</div><div class="line">    <span class="comment">#0: Binary</span></div><div class="line">    <span class="comment">#1: Binary Inverted</span></div><div class="line">    <span class="comment">#2: Threshold Truncated</span></div><div class="line">    <span class="comment">#3: Threshold to Zero</span></div><div class="line">    <span class="comment">#4: Threshold to Zero Inverted</span></div><div class="line">    threshold_type = <a class="code" href="../../d7/dfc/group__highgui.html#ga122632e9e91b9ec06943472c55d9cda8">cv.getTrackbarPos</a>(trackbar_type, window_name)</div><div class="line">    threshold_value = <a class="code" href="../../d7/dfc/group__highgui.html#ga122632e9e91b9ec06943472c55d9cda8">cv.getTrackbarPos</a>(trackbar_value, window_name)</div><div class="line">    _, dst = <a class="code" href="../../d7/d1b/group__imgproc__misc.html#gae8a4a146d1ca78c626a53577199e9c57">cv.threshold</a>(src_gray, threshold_value, max_binary_value, threshold_type )</div><div class="line">    <a class="code" href="../../df/d24/group__highgui__opengl.html#gaae7e90aa3415c68dba22a5ff2cefc25d">cv.imshow</a>(window_name, dst)</div></div><!-- fragment --> </div> <p>As you can see, the function <a class="el" href="../../d7/d1b/group__imgproc__misc.html#gae8a4a146d1ca78c626a53577199e9c57">cv::threshold</a> is invoked. We give \(5\) parameters in C++ code:</p>
<ul>
<li><em>src_gray</em>: Our input image</li>
<li><em>dst</em>: Destination (output) image</li>
<li><em>threshold_value</em>: The \(thresh\) value with respect to which the thresholding operation is made</li>
<li><em>max_BINARY_value</em>: The value used with the Binary thresholding operations (to set the chosen pixels)</li>
<li><em>threshold_type</em>: One of the \(5\) thresholding operations. They are listed in the comment section of the function above.</li>
</ul>
<h2>Results </h2>
<ol type="1">
<li><p class="startli">After compiling this program, run it giving a path to an image as argument. For instance, for an input image as:</p>
<div class="image">
<img src="../../Threshold_Tutorial_Original_Image.jpg" alt="Threshold_Tutorial_Original_Image.jpg"/>
</div>
</li>
<li><p class="startli">First, we try to threshold our image with a <em>binary threshold inverted</em>. We expect that the pixels brighter than the \(thresh\) will turn dark, which is what actually happens, as we can see in the snapshot below (notice from the original image, that the doggie's tongue and eyes are particularly bright in comparison with the image, this is reflected in the output image).</p>
<div class="image">
<img src="../../Threshold_Tutorial_Result_Binary_Inverted.jpg" alt="Threshold_Tutorial_Result_Binary_Inverted.jpg"/>
</div>
</li>
<li><p class="startli">Now we try with the <em>threshold to zero</em>. With this, we expect that the darkest pixels (below the threshold) will become completely black, whereas the pixels with value greater than the threshold will keep its original value. This is verified by the following snapshot of the output image:</p>
<div class="image">
<img src="../../Threshold_Tutorial_Result_Zero.jpg" alt="Threshold_Tutorial_Result_Zero.jpg"/>
</div>
 </li>
</ol>
</div></div><!-- contents -->
<!-- HTML footer for doxygen 1.8.6-->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated on Fri Apr 2 2021 11:36:36 for OpenCV by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="../../doxygen.png" alt="doxygen"/>
</a> 1.8.13
</small></address>
<script type="text/javascript">
//<![CDATA[
addTutorialsButtons();
//]]>
</script>
</body>
</html>
